package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.*;
import com.tt.fashionWebSite.domain.*;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.Ad_ProductService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("ad_productService")
public class Ad_ProductServiceImp extends GenericService implements Ad_ProductService {
    @Autowired
    @Qualifier("sanPhamFull")
    private SanPhamFullDao sanPhamFullDao;

    @Autowired
    @Qualifier("sanPham")
    private SanPhamDao sanPhamDao;

    @Autowired
    @Qualifier("chiTietSanPham")
    private ChiTietSanPhamDao chiTietSanPhamDao;

    @Autowired
    @Qualifier("binhLuan")
    private BinhLuanDao binhLuanDao;

    @Autowired
    @Qualifier("loaiDanhMucSanPham")
    private LoaiDanhMucSanPhamDao loaiDanhMucSanPhamDao;

    @Autowired
    @Qualifier("mauSac")
    private MauSacDao mauSacDao;

    @Autowired
    @Qualifier("kichThuoc")
    private KichThuocDao kichThuocDao;

    @Autowired
    @Qualifier("hoaTiet")
    private HoaTietDao hoaTietDao;

    @Override
    public List<BinhLuanDto> getProductComment() {
        return mapper.mapCollection(binhLuanDao.getProductComment(), BinhLuanDto.class);
    }

    public List<SanPhamDto> getAllProduct () {
        return mapper.mapCollection(sanPhamDao.getAllProduct(), SanPhamDto.class);
    }

    public List<LoaiDanhMucSanPhamDto> getAllProductListInProduct () {
        return mapper.mapCollection(loaiDanhMucSanPhamDao.getAllProductList(), LoaiDanhMucSanPhamDto.class);
    }

    public BinhLuanDto getProductCommentInfoEdit(BinhLuanDto commentDto){
        BinhLuan binhLuan = binhLuanDao.findById(commentDto.getMaBL());
        BinhLuanDto binhLuanDto = mapper.map(binhLuan, BinhLuanDto.class,BINH_LUAN_MAP_ID);
        return  binhLuanDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminProductCommon(SanPhamDto productDto) {
        SanPham sanPham = sanPhamDao.findById(productDto.getMaSP());
        if(sanPham == null){
            sanPham = new SanPham();
            sanPham.setMaSP(productDto.getMaSP());
            sanPham.setTenSP(productDto.getTenSP());
            sanPham.setDoiTuong(productDto.getDoiTuong());
            sanPham.setThongTin(productDto.getThongTin());
            LoaiDanhMucSanPham loaiDanhMucSanPham = loaiDanhMucSanPhamDao.findById(productDto.getLoaiDanhMucSanPham().getMaLoaiDMSP());
            sanPham.setLoaiDanhMucSanPham(loaiDanhMucSanPham);
            sanPhamDao.insert(sanPham);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public BinhLuanDto registerProductCommentEdit(BinhLuanDto commentDto) {
        BinhLuan binhLuan = binhLuanDao.findById(commentDto.getMaBL());
        try {
            binhLuan.setMaBL(commentDto.getMaBL());
            binhLuan.setEmail(commentDto.getEmail());
            binhLuan.setTen(commentDto.getTen());
            binhLuan.setNoiDung(commentDto.getNoiDung());
            SanPham sanPham = sanPhamDao.findById(commentDto.getSanPham().getMaSP());
            binhLuan.setSanPham(sanPham);
            binhLuanDao.update(binhLuan);
            return commentDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int addProductDetail(ChiTietSanPhamDto productDetailDto) {
        ChiTietSanPham chiTietSanPham = chiTietSanPhamDao.findById(productDetailDto.getMaCTSP());
        if(chiTietSanPham == null){
            chiTietSanPham = new ChiTietSanPham();
            chiTietSanPham.setMaCTSP(productDetailDto.getMaCTSP());
            chiTietSanPham.setGiaSP(productDetailDto.getGiaSP());
            chiTietSanPham.setSoLuong(productDetailDto.getSoLuong());

            KichThuoc kichThuoc = kichThuocDao.findById(productDetailDto.getKichThuoc().getMaSize());
            chiTietSanPham.setKichThuoc(kichThuoc);

            MauSac mauSac = mauSacDao.findById(productDetailDto.getMauSac().getMaMau());
            chiTietSanPham.setMauSac(mauSac);

            HoaTiet hoaTiet = hoaTietDao.findById(productDetailDto.getHoaTiet().getMaHoaTiet());
            chiTietSanPham.setHoaTiet(hoaTiet);

            SanPham sanPham = sanPhamDao.findById(productDetailDto.getSanPham().getMaSP());
            chiTietSanPham.setSanPham(sanPham);

            chiTietSanPhamDao.insert(chiTietSanPham);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    public SanPhamDto getProductInfoEdit(SanPhamDto productDto){
        SanPham sanPham = sanPhamDao.findById(productDto.getMaSP());
        SanPhamDto SanPhamDto = mapper.map(sanPham, SanPhamDto.class,SAN_PHAM_MAP_ID);
        return  SanPhamDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public SanPhamDto registerProductEdit(SanPhamDto productDto) {
        SanPham sanPham = sanPhamDao.findById(productDto.getMaSP());
        try {
            sanPham.setMaSP(productDto.getMaSP());
            sanPham.setTenSP(productDto.getTenSP());
            sanPham.setDoiTuong(productDto.getDoiTuong());
            sanPham.setThongTin(productDto.getThongTin());
            LoaiDanhMucSanPham loaiDanhMucSanPham = loaiDanhMucSanPhamDao.findById(productDto.getLoaiDanhMucSanPham().getMaLoaiDMSP());
            sanPham.setLoaiDanhMucSanPham(loaiDanhMucSanPham);
            sanPhamDao.update(sanPham);
            return productDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    public List<SanPhamDto> getProductWidthCategoryM(String idDMSP) {
        List<SanPhamDto> sanPhamDtos = new ArrayList<SanPhamDto>();
        List<SanPham> sanPhams = sanPhamDao.getProductWidthCategoryM(idDMSP);
        sanPhamDtos = mapper.mapCollection(sanPhams, SanPhamDto.class, SAN_PHAM_MAP_ID);
        return sanPhamDtos;
    }

    @Override
    public List<SanPhamDto> getProductWidthCategoryF(String idDMSP) {
        List<SanPhamDto> sanPhamDtos = new ArrayList<SanPhamDto>();
        List<SanPham> sanPhams = sanPhamDao.getProductWidthCategoryF(idDMSP);
        sanPhamDtos = mapper.mapCollection(sanPhams, SanPhamDto.class, SAN_PHAM_MAP_ID);
        return sanPhamDtos;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer deleteProductCommon(SanPhamDto sanPhamDto) {
        try{
            sanPhamDao.deleteProductCommon(sanPhamDto.getMaSP());
            return JsonResponseCode.SUCCESS;
        }
        catch (Exception ex) {
            return JsonResponseCode.FAILED;
        }
    }

}
